package com.hbgjq.security.filter;

import com.hbgjq.security.config.MyGrantedAuthority;
import com.hbgjq.security.po.UserPo;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :Gjq
 * @date :2021/1/8
 */
public class TokenFilter extends OncePerRequestFilter {

    /**
     * 保证每个请求只调用一次
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (request.getHeader("TOKEN") == null){
            response.setStatus(403);
            response.getWriter().println("please login ... SB LI QIANG");
            response.getWriter().flush();
            return;
        }
        SecurityContext context = SecurityContextHolder.getContext();
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("root", "root");
        context.setAuthentication(authRequest);
        filterChain.doFilter(request,response);
    }
}
